$(function () {
    $("#jqGrid").jqGrid({
        url: '../order/refundList',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '订单号', name: 'orderSn', index: 'order_sn', width: 150, align: 'center'},
            {label: '用户昵称', name: 'userEntity.nickname', width: 80, align: 'center'},
            {
                label: '订单状态',
                name: 'orderStatus',
                index: 'order_status',
                width: 80,
                align: 'center',
                formatter: function (value) {
                    if (value === 0) {
                        return '待付款';
                    } else if (value === 101) {
                        return '订单已取消';
                    } else if (value === 102) {
                        return '订单已删除';
                    } else if (value === 201) {
                        return '订单已付款';
                    } else if (value === 300) {
                        return '订单已发货';
                    } else if (value === 301) {
                        return '用户已收货';
                    } else if (value === 302) {
                        return '用户已评论';
                    } else if (value === 401) {
                        return '退款中';
                    } else if (value === 402) {
                        return '退款退货中';
                    } else if (value === 403) {
                        return '退款成功';
                    } else if (value === 404) {
                        return '退款失败';
                    }
                    return value;
                }
            },
            {
                label: '付款状态', name: 'payStatus', index: 'pay_status', width: 80, align: 'center',
                formatter: function (value) {
                    if (value === 0) {
                        return '未付款';
                    } else if (value === 1) {
                        return '付款中';
                    } else if (value === 2) {
                        return '已付款';
                    } else if (value === 4) {
                        return '退款';
                    }
                    return value;
                }
            },
            {
                label: '发货状态',
                name: 'shippingStatus',
                index: 'shipping_status',
                width: 60, align: 'center',
                formatter: function (value) {
                    if (value === 0) {
                        return '待发货';
                    } else if (value === 1) {
                        return '已发货';
                    } else if (value === 2) {
                        return '已收货';
                    } else if (value === 4) {
                        return '退货';
                    }
                    return value;
                }
            },
            {label: '订单总价', name: 'orderPrice', index: 'order_price', align: 'center', width: 60},
            {label: '实际支付金额', name: 'actualPrice', index: 'actual_price', align: 'center', width: 80},
            {
                label: '是否需要开票', name: 'isBilling', index: 'is_billing', width: 80, align: 'center',
                formatter: function (value) {
                    if (value === 0) {
                        return '<span class="label label-danger">否</span>';
                    } else if (value === 1) {
                        return '<span class="label label-info">是</span>';
                    }
                    return value;
                }
            },
            {
                label: '下单时间', name: 'addTime', index: 'add_time', width: 100, align: 'center',
                formatter: function (value) {
                    return transDate(value);
                }
            },
            {
                label: '申请退款时间', name: 'refundApplyTime', index: 'refund_apply_time', width: 100, align: 'center',
                formatter: function (value) {
                    return transDate(value);
                }
            },
            {
                label: '理由', name: 'refundRefuseReson', index: 'refundRefuseReson', width: 100, align: 'center'
            },
            {label: '买家备注', name: 'postscript', index: 'postscript', align: 'center', width: 60},
            {label: '卖家备注', name: 'remark', index: 'remark', align: 'center', width: 60},
            {
                label: '订单操作', width: 80, align: 'center', sortable: false, formatter: function (value, col, row) {
                    let sendGoods = (row.orderStatus === 401 || row.orderStatus === 402) ? '<span class="label label-warning" onclick="vm.agreeRefund(' + row.id + ')">同意</span>&nbsp;' : '';
                    let confirm = (row.orderStatus === 401 || row.orderStatus === 402) ? '<span class="label label-danger" onclick="vm.whyRefuseRefund(' + row.id + ')">拒绝</span>&nbsp;' : '';
                    return sendGoods + confirm === '' ? "暂无操作" : sendGoods + confirm;
                }
            },
            {
                label: '查看详情', width: 80, align: 'center', sortable: false, formatter: function (value, col, row) {
                    let detail = '<span class="label label-info"   onclick="vm.lookDetail(' + row.id + ')">查看详情</span><br>';
                    let showShippingDetail = (row.shippingStatus === 1 || row.shippingStatus === 2 || row.shippingStatus === 4) ? '<span class="label label-warning" onclick="vm.showShippingDetail(' + row.id + ')">查看物流</span><br>' : '';
                    let showInvoice = row.isBilling === 1 ? '<span class="label label-success" onclick="vm.showInvoice(' + row.id + ')">发票信息</span><br>' : '';
                    return detail + showInvoice + showShippingDetail;
                }
            }
        ],
        viewrecords: true,
        height: 485,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            q: {
                name: '',
                orderSn: ''
            },
            order: {
                // addTime: new Date()
            },
            modalRefuseRefund: false,
            orderId: null,
            refundRefuseReson: '',
            modalInvoice: false,
            modalDetail: false,
            orderTraces: [],
            modalShipping: false,
        },
        methods: {
            query: function () {
                vm.reload();
            },
            back: function (event) {
                var ids = getSelectedRows();
                if (ids == null) {
                    return;
                }

                confirm('确定要恢复选中的记录？', function () {
                    $.ajax({
                        type: "POST",
                        url: "../goods/back",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            },
            getInfo: function (id) {
                $.get("../goods/info/" + id, function (r) {
                    vm.goods = r.goods;
                    $('#goodsDesc').editable('setHTML', vm.goods.goodsDesc);
                    vm.getCategory();
                    vm.getAttributes(vm.goods.attributeCategory);
                });
            },
            reload: function (event) {
                vm.showList = true;
                var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {'name': vm.q.name, 'orderSn': vm.q.orderSn},
                    page: page
                }).trigger("reloadGrid");
                vm.handleReset('formValidate');
            },
            agreeRefund: function (rowId) {
                event.stopPropagation();
                let id = rowId;
                if (id == null) {
                    return;
                }
                confirm('确认操作吗？', function () {
                    $.ajax({
                        type: "POST",
                        url: '../order/agreeRefund',
                        data: {id: id},
                        success: function (r) {
                            if (r.code === 0) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            },
            refuseRefund: function () {
                event.stopPropagation();
                let id = vm.orderId;
                if (id == null) {
                    return;
                }
                let refundRefuseReson = vm.refundRefuseReson;

                $.ajax({
                    type: "POST",
                    url: '../order/refuseRefund',
                    data: {id: id, refundRefuseReson: refundRefuseReson},
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            },
            whyRefuseRefund: function (rowId) {
                vm.modalRefuseRefund = true;
                vm.orderId = rowId;
            },
            lookDetail: function (rowId) { //第三步：定义编辑操作
                event.stopPropagation();
                // vm.detail = true;
                vm.title = "详情";
                $.get("../order/info/" + rowId, function (r) {
                    vm.order = r.order;
                });
                vm.modalDetail = true;
            },
            showInvoice: function (rowId) {
                event.stopPropagation();
                $.get("../order/info/" + rowId, function (r) {
                    vm.order = r.order;
                });
                vm.modalInvoice = true;
            },
            showShippingDetail: function (rowId) {
                event.stopPropagation();
                vm.modalShipping = true;
                vm.getOrderTraces(rowId);
            },
            getOrderTraces: function (rowId) {
                let id = rowId;
                if (id == null) {
                    return;
                }
                $.get("../order/getOrderTraces?id=" + id, function (r) {
                    vm.orderTraces = r.data;
                });
            },
            orderUpdate: function (event) {
                $.ajax({
                    type: "POST",
                    url: '../order/update',
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(vm.order),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            },
            exportExcel: function () {
                let ids = getSelectedRows();
                if (ids == null) {
                    return;
                }
                this.$Notice.success({
                    desc: '请稍等片刻，文件正在生成中...'
                });
                exportFile('#rrapp', '../order/export', {'ids': ids});
            },
        }
    })
;